﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Maniac
{
    public class DayMatrix
    {
        private List<DayAggregate> ivlMonday;
        private List<DayAggregate> ivlTuesday;
        private List<DayAggregate> ivlWednesday;
        private List<DayAggregate> ivlThursday;
        private List<DayAggregate> ivlFriday;

        private List<DayAggregate> ivlMaster;

        public DayMatrix()
        {
            //initialize the lists
            this.ivlMonday = new List<DayAggregate>();
            this.ivlTuesday = new List<DayAggregate>();
            this.ivlWednesday = new List<DayAggregate>();
            this.ivlThursday = new List<DayAggregate>();
            this.ivlFriday = new List<DayAggregate>();
            this.ivlMaster = new List<DayAggregate>();
        }		
        public void InitializeLists()
        {
            this.ivlMonday.Clear();
            this.ivlTuesday.Clear();
            this.ivlWednesday.Clear();
            this.ivlThursday.Clear();
            this.ivlFriday.Clear();
            this.ivlMaster.Clear();

        }
        public void SortDays(Comparison<DayAggregate> comparison)
        {
            this.ivlMonday.Sort(comparison);
            this.ivlTuesday.Sort(comparison);
            this.ivlWednesday.Sort(comparison);
            this.ivlThursday.Sort(comparison);
            this.ivlFriday.Sort(comparison);          
        }
        public void CalculateWeekIncremental(DateTime startDate, SecuritySort sortType)
        {
            int tviIncrementalStart = startDate.DayOfYear;

            List<DayAggregate> tvdList = ivlMaster.GetRange(tviIncrementalStart, 5);
            foreach (DayAggregate tvdDay in tvdList)
            {
                if (!DateTime.IsLeapYear(startDate.Year) && tvdDay.Month == 2 && tvdDay.Day == 29)
                    continue;
                DateTime tvdTest = new DateTime(startDate.Year, tvdDay.Month, tvdDay.Day);
                AddToDayAggregate(tvdTest.DayOfWeek, tvdDay);               
            }
            SortDays(DayAggregateSorter.GetSort(sortType));
        }
        public void CalculateWeekCalendar(DateTime startDate, DateTime endDate, SecuritySort sortType)
        {
            //Based on the input Start and End, determine the correct
            //lists for each day. Next, sort by the inpuy type.
            foreach (DayAggregate tvdDay in Master)
            {
                if (!DateTime.IsLeapYear(startDate.Year) && tvdDay.Month == 2 && tvdDay.Day == 29)
                    continue;
                DateTime tvdTest = new DateTime(startDate.Year, tvdDay.Month, tvdDay.Day);
                if ((tvdTest.CompareTo(startDate) >= 0) && (tvdTest.CompareTo(endDate) <= 0))
                {
                    AddToDayAggregate(tvdTest.DayOfWeek, tvdDay);                   
                }
            }
            //SortEachDay.
            SortDays(DayAggregateSorter.GetSort(sortType));
        }

        private void AddToDayAggregate(DayOfWeek dayOfWeek, DayAggregate tvdDay)
        {
            switch (dayOfWeek)
            {
                case DayOfWeek.Monday:
                    {
                        ivlMonday.Add(tvdDay);
                        break;
                    }
                case DayOfWeek.Tuesday:
                    {
                        ivlTuesday.Add(tvdDay);
                        break;
                    }
                case DayOfWeek.Wednesday:
                    {
                        ivlWednesday.Add(tvdDay);
                        break;
                    }
                case DayOfWeek.Thursday:
                    {
                        ivlThursday.Add(tvdDay);
                        break;
                    }
                case DayOfWeek.Friday:
                    {
                        ivlFriday.Add(tvdDay);
                        break;
                    }
            }
        }
        
        public List<DayAggregate> Monday
        {
            get
            {
                return ivlMonday;
            }
        }
        public List<DayAggregate> Tuesday
        {
            get
            {
                return ivlTuesday;
            }
        }
        public List<DayAggregate> Wednesday
        {
            get
            {
                return ivlWednesday;
            }
        }
        public List<DayAggregate> Thursday
        {
            get
            {
                return ivlThursday;
            }
        }
        public List<DayAggregate> Friday
        {
            get
            {
                return ivlFriday;
            }
        }
        public List<DayAggregate> Master
        {
            get
            {
                return ivlMaster;
            }
        }
    }
}
